<?php

namespace App\Filament\Resources;

use App\Filament\Resources\CategoryResource\Pages;
use App\Filament\Resources\CategoryResource\RelationManagers;
use App\Filament\Resources\CategoryResource\Widgets\CategoryOverview;
use App\Models\Category;
use BezhanSalleh\FilamentShield\Contracts\HasShieldPermissions;
use CodeWithDennis\FilamentSelectTree\SelectTree;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;

class CategoryResource extends Resource implements HasShieldPermissions
{
    protected static ?string $model = Category::class;

    protected static ?string $modelLabel = '分类';

    protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';

    protected static ?string $navigationLabel = '图书分类';
    protected static ?string $navigationGroup = '图书管理';
    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                //
                Forms\Components\TextInput::make('name')
                    ->autofocus()
                    ->required()
                    ->label('分类名称'),
                Forms\Components\TextInput::make('slug')
                    ->autofocus()
                    ->default('')
                    ->label('分类别名'),
                SelectTree::make('parent_id')
                    ->label('父级分类')
                    ->relationship('category', 'name','parent_id')
                    ->enableBranchNode()
                    ->emptyLabel('没有相关数据呢')
                    ->placeholder('请选择父级分类'),
                Forms\Components\FileUpload::make('img')
                    ->default('')
                    ->autofocus()
                    ->image()
                    ->imageEditor()
                    ->label('分类图片'),
                Forms\Components\TextInput::make('sort')
                    ->numeric()
                    ->default(100)
                    ->label('排序'),
                Forms\Components\Toggle::make('status')
                    ->default(true)
                    ->autofocus()
                    ->label('是否显示'),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                //
                Tables\Columns\TextColumn::make('name')
                    ->label('分类名称')
                    ->searchable(),
                Tables\Columns\TextColumn::make('sort')
                    ->badge()
                    ->sortable()
                    ->label('排序'),
                Tables\Columns\ToggleColumn::make('status')
                    ->label('是否显示'),
                Tables\Columns\TextColumn::make('created_at')
                    ->label('创建时间'),

            ])
            ->modifyQueryUsing(fn(Builder $query) => $query->withoutGlobalScope(SoftDeletingScope::class))
            ->filters([
                //
            ])
            ->actions([
                Tables\Actions\EditAction::make(),
                Tables\Actions\DeleteAction::make(),
            ])
            ->paginated(false)
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make(),
                ]),
            ]);
    }

    public static function getRelations(): array
    {
        return [
            //
        ];
    }


    public static function getPages(): array
    {
        return [
            'index' => Pages\ListCategories::route('/'),
            'create' => Pages\CreateCategory::route('/create'),
            'edit' => Pages\EditCategory::route('/{record}/edit'),
            'tree' => Pages\TreeList::route('/tree'),
        ];
    }

    public static function getPermissionPrefixes(): array
    {
        return [
            'view',
            'view_any',
            'create',
            'update',
            'delete',
            'delete_any',
            'tree'
        ];
    }

    public static function getWidgets(): array
    {
        return [
            CategoryOverview::class
        ];
    }

}
